﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Waiter.Common;
using DevComponents.DotNetBar.Controls;
using Waiter.DataLayer;

namespace Waiter.Controler
{
    class PaymentCtrl
    {
        /// <summary>
        /// Phương thức lưu data vào table PAYMENT (thông tin chi phí)
        /// </summary>
        /// <param name="pPayClassCode"></param>
        /// <param name="pItemCode"></param>
        /// <param name="pPayMemberCode"></param>
        /// <param name="pAmount"></param>
        /// <param name="pNote"></param>
        /// <param name="pCreateDate"></param>
        /// <param name="pAddDate"></param>
        /// <returns></returns>
        public string fsInserPayment(string pPayClassCode,
                                     string pItemCode,
                                     string pPayMemberCode,
                                     long pAmount,
                                     string pNote,
                                     DateTime pCreateDate,
                                     string pOPID,
                                     DateTime pAddDate)
        {

            string strCode = String.Format(CommonConstants.CODE_DATE, DateTime.Now);
            PAYMENT myPayment = new PAYMENT();
            try
            {
                myPayment.CODE = strCode;
                myPayment.PAY_CLASS_CODE = pPayClassCode;
                myPayment.ITEM_CODE = pItemCode;
                myPayment.PAY_MEMBER_CODE = pPayMemberCode;
                myPayment.AMOUNT = pAmount;
                myPayment.NOTE = pNote;
                myPayment.CREATE_DATE = pCreateDate;
                myPayment.DEL_FLG = false;
                myPayment.OPID = pOPID;
                myPayment.ADD_DATE = pAddDate;
                myPayment.UPD_DATE = pAddDate;

                CommonGlobalVariable.globalDataContext.PAYMENTs.InsertOnSubmit(myPayment);
                CommonGlobalVariable.globalDataContext.SubmitChanges();

                return string.Empty;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                  "PaymentCtrl.fsInserPayment",
                  CommonUtils.fsParamListToString(new object[1] { myPayment }),
                  ex.ToString());
                return ex.Message;
            }
        }

        /// <summary>
        /// Phương thức thay đổi data của table PAYMENT
        /// </summary>
        /// <param name="strCode"></param>
        /// <param name="pItemCode"></param>
        /// <param name="pPayMemberCode"></param>
        /// <param name="pAmount"></param>
        /// <param name="pNote"></param>
        /// <param name="pCreateDate"></param>
        /// <param name="pOPID"></param>
        /// <param name="pAddDate"></param>
        /// <returns></returns>
        public string fsUpdatePayment(string strCode,
                                      string pPayClassCode,
                                      string pItemCode,
                                      string pPayMemberCode,
                                      long pAmount,
                                      string pNote,
                                      DateTime pCreateDate,
                                      string pOPID,
                                      DateTime pAddDate)
        {

            PAYMENT myPayment = CommonGlobalVariable.globalDataContext.PAYMENTs.SingleOrDefault(x => x.CODE == strCode);
            try
            {
                if (myPayment != null)
                {
                    myPayment.PAY_CLASS_CODE = pPayClassCode;
                    myPayment.ITEM_CODE = pItemCode;
                    myPayment.PAY_MEMBER_CODE = pPayMemberCode;
                    myPayment.AMOUNT = pAmount;
                    myPayment.NOTE = pNote;
                    myPayment.CREATE_DATE = pCreateDate;
                    myPayment.DEL_FLG = false;
                    myPayment.OPID = pOPID;
                    myPayment.UPD_DATE = pAddDate;

                    CommonGlobalVariable.globalDataContext.SubmitChanges();

                    return string.Empty;
                }
                else
                {
                    return "Không tìm thấy dữ liệu cần cập nhật";
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                  "PaymentCtrl.fsUpdatePayment",
                  CommonUtils.fsParamListToString(new object[1] { myPayment }),
                  ex.ToString());
                return ex.Message;
            }
        }

        /// <summary>
        /// Phương thức xóa data PAYMENT
        /// </summary>
        /// <param name="strCode"></param>
        /// <returns></returns>
        public string fsDeletePayment(string strCode)
        {
            try
            {
                PAYMENT myPayment = CommonGlobalVariable.globalDataContext.PAYMENTs.SingleOrDefault(x => x.CODE == strCode);

                if (myPayment != null)
                {
                    CommonGlobalVariable.globalDataContext.PAYMENTs.DeleteOnSubmit(myPayment);
                    CommonGlobalVariable.globalDataContext.SubmitChanges();

                    return string.Empty;
                }
                else
                {
                    return "Không tìm thấy dữ liệu cần xóa";
                }
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                  "PaymentCtrl.fsDeletePayment",
                  CommonUtils.fsParamListToString(new object[1] { strCode }),
                  ex.ToString());
                return ex.Message;
            }
        }

        /// <summary>
        /// Load danh sách data vào grid
        /// </summary>
        /// <param name="pPayClassCode">Code loại chi phí (nếu chọn tất cả là string.empty)</param>
        /// <param name="pItemCode">Code hạng mục (nếu chọn tất cả là string.empty)</param>
        /// <param name="pDateFrom">Thời điểm chi trả bắt đầu (nếu chọn tất cả DateTime.MinDate</param>
        /// <param name="pDateTo">Thời điểm chi trả kết thúc</param>
        /// <returns></returns>
        public List<VIEW_Payment> fsLoadGridData(string pPayClassCode, string pItemCode, DateTime pDateFrom, DateTime pDateTo)
        {
            try
            {
                List<VIEW_Payment> myListPayment = (from p in CommonGlobalVariable.globalDataContext.PAYMENTs
                                                    select new VIEW_Payment
                                                    {
                                                        strCODE = p.CODE,
                                                        strPAY_CLASS_CODE = p.PAY_CLASS_CODE,
                                                        strPAY_CLASS_NAME = CommonGlobalVariable.globalDataContext.MASTERs.SingleOrDefault(x=>x.CODE == p.PAY_CLASS_CODE).VALUE,
                                                        strITEM_CODE = p.ITEM_CODE,
                                                        strITEM_NAME = CommonGlobalVariable.globalDataContext.MASTERs.SingleOrDefault(x => x.CODE == p.ITEM_CODE).VALUE,                          
                                                        strPAY_MEMBER_CODE = p.PAY_MEMBER_CODE,
                                                        strPAY_MEMBER_NAME = CommonGlobalVariable.globalDataContext.STAFFs.SingleOrDefault(x => x.CODE == p.PAY_MEMBER_CODE & x.DEL_FLG == false).NAME,                          
                                                        nAMOUNT = p.AMOUNT,
                                                        strNOTE = p.NOTE,
                                                        dateCREATE_DATE = p.CREATE_DATE,
                                                        bDEL_FLG = p.DEL_FLG,
                                                        strOPID = p.OPID,
                                                        dateADD_DATE = p.ADD_DATE,
                                                        dateUPD_DATE = p.UPD_DATE
                                                    }).ToList();

                if (pPayClassCode != string.Empty)
                {
                    myListPayment = myListPayment.Where(x => x.strPAY_CLASS_CODE == pPayClassCode).ToList();
                }

                if (pItemCode != string.Empty)
                {
                    myListPayment = myListPayment.Where(x => x.strITEM_CODE == pItemCode).ToList();
                }

                if (pDateFrom != DateTime.MinValue)
                {
                    DateTime dateStart = new DateTime(pDateFrom.Year, pDateFrom.Month, pDateFrom.Day, 0, 0, 0);
                    DateTime dateEnd = new DateTime(pDateFrom.Year, pDateFrom.Month, pDateFrom.Day, 23, 59, 59);
                    myListPayment = myListPayment.Where(x => x.dateCREATE_DATE >= dateStart & x.dateCREATE_DATE <= dateEnd).ToList();
                }

                return myListPayment;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                  "PaymentCtrl.fsLoadGridData",
                  CommonUtils.fsParamListToString(new object[4] { pPayClassCode, pItemCode, pDateFrom, pDateTo }),
                  ex.ToString());
                return null;
            }
        }
    }
}
